import Vue from 'vue'
import VueRouter from 'vue-router'

import Home from "@/components/common/Home.vue";
import TableData from "@/views/table/TableData.vue";

Vue.use(VueRouter)

const routes = [
    {
        path: "/",
        redirect: "/home",
    },
    {
        path: '/home',
        name: 'home',
        component: Home,
        redirect: "/table_data",
        children: [
            {
                path: "/table_data",
                component: TableData
            },
        ]
    },
    {
        path: '*',
        name: '/404',
        component: () => import('../views/error/404.vue')
    }
]

const viewFiles = require.context("../views/view", true, /\.vue$/);

// 动态生成 children 路由
viewFiles.keys().forEach(key => {
    const component = viewFiles(key).default;
    const path = key.replace(/^\.\/(.*)\.\w+$/, '$1');

    routes[1].children.push({
        path: `/${path}`,
        component,
    });
});

const router = new VueRouter({
    routes,
    scrollBehavior: () => ({y: 0}),
})


export default router